#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include    <QDir>
#include    <QFileDialog>
#include <QDesktopServices>
#include <QToolBar>
#include <QT>
#include <QVariant>
#include <QHBoxLayout>
#include <QProcess>
#include <QSpinBox>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QApplication::setWindowIcon(QIcon(":/icon/image/embedded.png"));
    UI_Init();
    connectSignalsSlots();
    //程序入口处
    QString strPath = QCoreApplication::applicationDirPath() + "/UILayout.ini";
    QFile file(strPath);
    if(file.open(QIODevice::ReadOnly)) {
        QByteArray ba;
        QDataStream in(&file);
        in>>ba;
        file.close();
        this->restoreState(ba);
    }

}

MainWindow::~MainWindow()
{
    //程序出口处
    QString strPath = QCoreApplication::applicationDirPath() + "/UILayout.ini";
    QFile file(strPath);
    if(file.open(QIODevice::WriteOnly))
    {
        QDataStream outfile(&file);
        QByteArray ba = this->saveState();
        outfile<<ba;
        file.close();
    }
    delete ui;
}






void MainWindow::on_actionExit_triggered()
{

}




/**
 * @brief MainWindow::tabHex2BinCreateItemARow  为hex2bin表格创建一行
 * @param rowNo 行号
 * @param isCheck 是否复选
 */
void MainWindow::tabHex2BinCreateItemARow(int rowNo,bool isCheck)
{
    QTableWidgetItem *item = nullptr;
    Qt::ItemFlags flags;

    item=new  QTableWidgetItem(MainWindow::cSelect);
    item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    flags = Qt::ItemIsUserCheckable |Qt::ItemIsEnabled;   //不允许编辑，但可以更改复选状态
    item->setFlags(flags);
    if (isCheck)
        item->setCheckState(Qt::Checked);
    else
        item->setCheckState(Qt::Unchecked);
    item->setData( Qt::UserRole,"");
    ui->tableWidget_Hex2Bin->setItem(rowNo,0,item);

    QIcon   icon;
    icon.addFile(":/icon/image/OpenFile.png");
    item = new  QTableWidgetItem(MainWindow::cInput);
        item->setToolTip("双击单元格导入hex文件");
    item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    item->setIcon(icon);
    flags = Qt::ItemIsEnabled;    //不允许编辑
    item->setFlags(flags);
    item->setData( Qt::UserRole,"");
    ui->tableWidget_Hex2Bin->setItem(rowNo,1,item);

    QIcon   iconOpenFolder;
    iconOpenFolder.addFile(":/icon/image/OpenFolder.png");
    item = new  QTableWidgetItem(MainWindow::cOutput);
    item->setToolTip("双击单元格打开文件夹");
    item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    item->setIcon(iconOpenFolder);
    flags = Qt::ItemIsEnabled;    //不允许编辑
    item->setFlags(flags);
    item->setData( Qt::UserRole,"");
    ui->tableWidget_Hex2Bin->setItem(rowNo,2,item);

}

/**
 * @brief MainWindow::tabBinMergeCreateItemARow 为binmerge表格创建一行
 * @param rowNo 行号
 * @param isCheck 是否复选
 */
void MainWindow::tabBinMergeCreateItemARow(int rowNo,bool isCheck)
{
    QTableWidgetItem *item = nullptr;
    Qt::ItemFlags flags;

    item =new  QTableWidgetItem(MainWindow::cSelect);
    item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    flags = Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;   //不允许编辑，但可以更改复选状态
    item->setFlags(flags);
    if (isCheck)
        item->setCheckState(Qt::Checked);
    else
        item->setCheckState(Qt::Unchecked);
    item->setData( Qt::UserRole,"");
    ui->tableWidget_BinMerge->setItem(rowNo,0,item);

    QIcon   icon;
    icon.addFile(":/icon/image/OpenFile.png");
    item = new  QTableWidgetItem(MainWindow::cInput);
    item->setToolTip("双击单元格导入hex文件");
    item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    item->setIcon(icon);
    flags = Qt::ItemIsEnabled;    //不允许编辑
    item->setFlags(flags);
    item->setData( Qt::UserRole,"");
    ui->tableWidget_BinMerge->setItem(rowNo,1,item);

    item=new  QTableWidgetItem(MainWindow::cAddress);
    item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    flags = Qt::ItemIsEnabled;   //不允许编辑
    item->setData( Qt::UserRole,"");
    QSpinBox* spinbox= new QSpinBox(ui->tableWidget_BinMerge);
    spinbox->setPrefix("0x");
    spinbox->setMaximum(0x7FFFFFFF);
    spinbox->setDisplayIntegerBase(16);
    spinbox->setButtonSymbols(QSpinBox::UpDownArrows);
    ui->tableWidget_BinMerge->setCellWidget(rowNo,2,spinbox);
    ui->tableWidget_BinMerge->setItem(rowNo,2,item);

}

/**
 * @brief MainWindow::tabHex2BinInit hex2bin表格初始化
 */
void MainWindow::tabHex2BinInit()
{
    for (int i=0; i<ui->tableWidget_Hex2Bin->rowCount(); i++)
    {
        tabHex2BinCreateItemARow(i,false);
    }
}

/**
 * @brief MainWindow::tabBinMergeInit binmerge表格初始化
 */
void MainWindow::tabBinMergeInit()
{
    for (int i=0; i<ui->tableWidget_BinMerge->rowCount(); i++)
    {
        tabBinMergeCreateItemARow(i,false);
    }
}

/**
 * @brief MainWindow::getTableObjectByTabIndex  获取tab页的table
 * @return
 */
QTableWidget *MainWindow::getTableObjectByTabIndex()
{
    int index = this->ui->tabWidget->currentIndex();
    QTableWidget* table = nullptr;
    if(index == 0){     //hex2bin
        table = ui->tableWidget_Hex2Bin;
    }else if(index == 1){//binMerge
        table = ui->tableWidget_BinMerge;
    }else if(index == 2){//添加检验
        table = nullptr;
    }
    return table;
}

void MainWindow::on_actionResetWindowLayout_triggered()
{


}


/**
 * @brief MainWindow::on_tableWidget_Hex2Bin_cellDoubleClicked hex2bin表格信号处理函数
 * @param row
 * @param column
 */
void MainWindow::on_tableWidget_Hex2Bin_cellDoubleClicked(int row, int column)
{
    QTableWidgetItem  *item = ui->tableWidget_Hex2Bin->item( row,  column);          //通过行号和列号返回项
    int cellType= item->type();
    if(cellType == MainWindow::cSelect){    //选择
        Qt::CheckState isCheck = item->checkState();
        if (isCheck != Qt::Checked)
            item->setCheckState(Qt::Checked);
        else
            item->setCheckState(Qt::Unchecked);
    }else if(cellType == MainWindow::cInput){//输入hex
        QStringList fileList = getFileListFromFolder();
        int size = fileList.size();
        int index = 0;
        for(;index < size;index++){
            if(index != 0){
                ui->tableWidget_Hex2Bin->insertRow(row+index);
                tabHex2BinCreateItemARow(row+index,true);
                item = ui->tableWidget_Hex2Bin->item( row+index,  1);
            }else{
                QTableWidgetItem  *itemCheck = ui->tableWidget_Hex2Bin->item( row,  0);
                itemCheck->setCheckState(Qt::Checked);
            }
            item->setData(Qt::UserRole,QVariant(fileList[index]));
            QFileInfo  fileInfo(fileList[index]);
            QString text = fileInfo.fileName();
            QString textBrowserString = "add file:" + fileList[index];
            this->ui->textBrowser->append(textBrowserString);
            // this->ui->textBrowser->setPlainText(text);
            item->setText(text);
        }

    }else if(cellType == MainWindow::cOutput){      //打开bin
        QString dest = item->data(Qt::UserRole).toString();
        if(dest.isEmpty())return;
        QFileInfo  fileInfo(dest);
        QDir::setCurrent(fileInfo.absolutePath());  //设置应用程序当前目录
        QUrl url = QUrl(QDir::currentPath(), QUrl::TolerantMode);
        QDesktopServices::openUrl(url);
    }else{
        ;
    }

}

/**
 * @brief MainWindow::on_actionTransitioningContent_triggered 开始转化工作
 */
void MainWindow::on_actionTransitioningContent_triggered()
{
    int tabIndex = this->ui->tabWidget->currentIndex();
    if(tabIndex == 0){//hex2bin
        int rowCount = ui->tableWidget_Hex2Bin->rowCount();
        for(int i = 0; i < rowCount; i++){
            if(ui->tableWidget_Hex2Bin->item( i,  0)->checkState() == Qt::Checked){
                QTableWidgetItem  *item = ui->tableWidget_Hex2Bin->item( i,  1);          //通过行号和列号返回项
                QString src = item->data(Qt::UserRole).toString();
                if(src.isEmpty())continue;
                int N = src.lastIndexOf(".hex");
                QString dst = src;
                QString subStr= ".bin";
                dst.replace(N,subStr.size(),subStr);
                qDebug() << "src:" << src << "dst:" << dst;
                QStringList   parameter;
                parameter << src  << dst;
                qDebug() << parameter;
                QString  exePath =  QCoreApplication::applicationDirPath();
                exePath.append("/bin/hex2bin.exe");
                if(QProcess::execute(exePath,parameter)){//error
                    qDebug("An error occurred while running the program(hex2bin.exe)");
                }else{//succeed
                    item = ui->tableWidget_Hex2Bin->item( i,  2);
                    item->setData(Qt::UserRole,QVariant(dst));
                    QFileInfo  fileInfo(dst);
                    QString text = fileInfo.fileName();
                    QString textBrowserString = "hex file to bin file success:" + dst;
                    this->ui->textBrowser->append(textBrowserString);
                    // this->ui->textBrowser->setPlainText(text);
                    item->setText(text);
                }
            }

        }
    }else if(tabIndex == 1){//binMerge
        int rowCount = ui->tableWidget_BinMerge->rowCount();
        QStringList fileList, offsetList, parameter;
        for(int i = 0; i < rowCount; i++){
            if(ui->tableWidget_BinMerge->item( i,  0)->checkState() == Qt::Checked){
                QTableWidgetItem  *item = ui->tableWidget_BinMerge->item( i,  1);          //通过行号和列号返回项
                QString src = item->data(Qt::UserRole).toString();
                if(src.isEmpty())continue;
                fileList << src;
                QWidget *widget = ui->tableWidget_BinMerge->cellWidget(i,2);
                QSpinBox *spinbox= qobject_cast<QSpinBox*>(widget);
                offsetList << spinbox->text();
            }
        }
        if(fileList.size() == 0)return;
        QTime time = QTime::currentTime();
        QString timeString = time.toString("HH:mm:ss");
        QDate date = QDate::currentDate();
        QString dateString = date.toString("yyyy-MM-dd");
        QString outFile = dateString +"-" + timeString + ".bin";
        for(int i = 0;i < outFile.size();i++){
            if(outFile[i] == ':')outFile[i] = '-';
        }
        qDebug()<<outFile;
        parameter = fileList + offsetList;
        int N= fileList[0].lastIndexOf("/");
        int M= fileList[0].lastIndexOf("\\");
        if(N > 0){
            outFile.prepend(fileList[0].mid(0,N+1));
        }else if(M > 0){
            outFile.prepend(fileList[0].mid(0,M+1));
        }
        binMergeOpenFloder_PushButton->setProperty("binMergeFilePath",outFile);
        parameter << outFile;
        QString  exePath =  QCoreApplication::applicationDirPath();
        exePath.append("/bin/binMerge.exe");
        if(QProcess::execute(exePath,parameter)){//error
            qDebug("An error occurred while running the program(binMerge.exe)");
        }else{
            qDebug("merge bin successful");
            outFile.append(" is generated successfully!");
            this->ui->textBrowser_2->append(outFile);
            binMergeOpenFloder_PushButton->setEnabled(true);
        }
    }else if(tabIndex == 2){//添加检验
        ;
    }
}

/**
 * @brief MainWindow::on_pushButton_transmit_clicked 开始hex转bin
 */
void MainWindow::on_pushButton_transmit_clicked()
{
    on_actionTransitioningContent_triggered();
}

/**
 * @brief MainWindow::on_actionAddToRowAbove_triggered 在上方添加一行
 */
void MainWindow::on_actionAddToRowAbove_triggered()
{
    QTableWidget* table = getTableObjectByTabIndex();
    if(table == nullptr)return;
    int curRow = table->currentRow();     //当前行号
    table->insertRow(curRow);             //插入一行，但不会自动为单元格创建item
    int index = this->ui->tabWidget->currentIndex();
    if(index == 0){                     //hex2bin
        tabHex2BinCreateItemARow(curRow,false);
    }else if(index == 1){               //binMerge
        tabBinMergeCreateItemARow(curRow,false);
    }else if(index == 2){               //添加检验
        ;
    }
}

/**
 * @brief MainWindow::on_actionAddToRowBelow_triggered 在下方插入行
 */
void MainWindow::on_actionAddToRowBelow_triggered()
{
    QTableWidget* table = getTableObjectByTabIndex();
    if(table == nullptr)return;
    int curRow = table->currentRow();     //当前行号
    table->insertRow(curRow+1);           //插入一行，但不会自动为单元格创建item
    int index = this->ui->tabWidget->currentIndex();
    if(index == 0){                     //hex2bin
        tabHex2BinCreateItemARow(curRow+1,false);
    }else if(index == 1){               //binMerge
        tabBinMergeCreateItemARow(curRow+1,false);
    }else if(index == 2){               //添加检验
        ;
    }
}

/**
 * @brief MainWindow::on_actionAddToRowBottom_triggered 在底部增加行
 */
void MainWindow::on_actionAddToRowBottom_triggered()
{
    QTableWidget* table = getTableObjectByTabIndex();
    if(table == nullptr)return;
    int curRow = table->rowCount();     //当前行号
    table->insertRow(curRow);           //插入一行，但不会自动为单元格创建item
    int index = this->ui->tabWidget->currentIndex();
    if(index == 0){                     //hex2bin
        tabHex2BinCreateItemARow(curRow,false);
    }else if(index == 1){               //binMerge
        tabBinMergeCreateItemARow(curRow,false);
    }else if(index == 2){               //添加检验
        ;
    }

}

/**
 * @brief MainWindow::on_actionDeteleRow_triggered 删除所选行
 */
void MainWindow::on_actionDeteleRow_triggered()
{
    QTableWidget* table = getTableObjectByTabIndex();
    if(table == nullptr)return;
    int curRow = table->currentRow();     //当前行号
    table->removeRow(curRow);             //删除当前行及其items
}

/**
 * @brief MainWindow::on_actionOpenfileDialog_triggered 返回多个符合条件的文件
 * @return
 */
QStringList MainWindow::on_actionOpenfileDialog_triggered()
{//选择打开多个文件
    QString curPath=QDir::currentPath();//获取系统当前目录
    QString dlgTitle="打开多个文件";      //对话框标题
    int index = this->ui->tabWidget->currentIndex();
    QString filters;
    if(index == 0){                     //hex2bin
        filters = "hex文件(*.hex)";
    }else if(index == 1){               //binMerge
        filters = "bin文件(*.bin)";
    }else if(index == 2){               //添加检验
        filters = "bin文件(*.bin);;hex文件(*.hex)";
    }

    QStringList fileList=QFileDialog::getOpenFileNames(this,dlgTitle,curPath,filters);
    if(!fileList.size())return fileList;
    int empty_cell = 0;
    QTableWidget* table = getTableObjectByTabIndex();
    if(table == nullptr){
        fileList.clear();
        return fileList;
    }
    for(int j = 0; j < table->rowCount(); j++){
        QTableWidgetItem  *item = table->item( j,  1);              //通过行号和列号返回项
        QString src = item->data(Qt::UserRole).toString();
        if(src.isEmpty()){
            empty_cell++;
        }
    }
    for(int i = 0;i < fileList.size() - empty_cell;i++){
        int row = table->rowCount();
        table->insertRow(row);
        if(index == 0){                 //hex2bin
            tabHex2BinCreateItemARow(row,false);
        }else if(index == 1){           //binMerge
            tabBinMergeCreateItemARow(row,false);
        }else if(index == 2){           //添加检验
            ;
        }
    }
    QFileInfo  fileInfo(fileList.at(0));
    QDir::setCurrent(fileInfo.absolutePath());                      //设置应用程序当前目录
    for (int i=0; i<fileList.size();i++){
        QFileInfo  fileInfoAdd(fileList.at(i));
        int row = table->rowCount();
        for(int j = 0; j < row; j++){
            QTableWidgetItem  *item = table->item( j,  1);          //通过行号和列号返回项
            QString src = item->data(Qt::UserRole).toString();
            if(src.isEmpty()){
                QTableWidgetItem  *itemCheck = table->item( j,  0);
                itemCheck->setCheckState(Qt::Checked);
                item->setData(Qt::UserRole,QVariant(fileInfoAdd.absoluteFilePath()));
                QString text = fileInfoAdd.fileName();
                QString textBrowserString = "add file:" + fileInfoAdd.absoluteFilePath();
                if(index == 0){                 //hex2bin
                    this->ui->textBrowser->append(textBrowserString);
                }else if(index == 1){           //binMerge
                    this->ui->textBrowser_2->append(textBrowserString);
                }else if(index == 2){           //添加检验
                    ;
                }
                item->setText(text);
                qDebug() << "add file:" << fileList[i] << "to table";
                break;
            }
        }
    }
    return fileList;

}
/**
 * @brief MainWindow::getFileListFromFolder 得到当前文件夹下符合条件的文件
 * @return
 */
QStringList MainWindow::getFileListFromFolder()
{
    QString curPath=QDir::currentPath();//获取系统当前目录
    QString dlgTitle="打开多个文件";      //对话框标题
    int index = this->ui->tabWidget->currentIndex();
    QString filters;
    if(index == 0){                     //hex2bin
        filters = "hex文件(*.hex)";
    }else if(index == 1){               //binMerge
        filters = "bin文件(*.bin)";
    }else if(index == 2){               //添加检验
        filters = "bin文件(*.bin);;hex文件(*.hex)";
    }

    QStringList fileList=QFileDialog::getOpenFileNames(this,dlgTitle,curPath,filters);
    if(!fileList.size())return fileList;
    int empty_cell = 0;
    QTableWidget* table = getTableObjectByTabIndex();
    if(table == nullptr){
        fileList.clear();
        return fileList;
    }

    QFileInfo  fileInfo(fileList.at(0));
    QDir::setCurrent(fileInfo.absolutePath());                      //设置应用程序当前目录

    return fileList;
}

/**
 * @brief MainWindow::on_actionFolderOpened_triggered 将所选文件夹下的所有符合条件的文件添加到表格中
 */
void MainWindow::on_actionFolderOpened_triggered()
{
    QString curPath=QDir::currentPath();//获取系统当前目录
    QString dlgTitle="选择一个目录";      //对话框标题
    QString selectedDir=QFileDialog::getExistingDirectory(this,dlgTitle,curPath);
    if (!selectedDir.isEmpty()){
        QDir::setCurrent(selectedDir);
        qDebug()<< "open folder:" << selectedDir;
        QDir dir;
        dir.setFilter(QDir::Files);
        QStringList filters;
        int index = this->ui->tabWidget->currentIndex();
        if(index == 0){                 //hex2bin
            filters << "*.hex";
        }else if(index == 1){           //binMerge
            filters << "*.bin";
        }else if(index == 2){           //添加检验
            filters << "*.bin" << "*.hex";
        }

        dir.setNameFilters(filters);
        dir.setSorting(QDir::Name);
        QFileInfoList infoList = dir.entryInfoList();
        if(!infoList.size())return;
        int empty_cell = 0;
        QTableWidget* table = getTableObjectByTabIndex();
        if(table == nullptr){
            return ;
        }
        for(int j = 0; j < table->rowCount(); j++){
            QTableWidgetItem  *item = table->item( j,  1);          //通过行号和列号返回项
            QString src = item->data(Qt::UserRole).toString();
            if(src.isEmpty()){
                empty_cell++;
            }
        }
        for(int i = 0;i < infoList.size() - empty_cell;i++){
            int row = table->rowCount();
            table->insertRow(row);
            if(index == 0){                 //hex2bin
                tabHex2BinCreateItemARow(row,false);
            }else if(index == 1){           //binMerge
                tabBinMergeCreateItemARow(row,false);
            }else if(index == 2){           //添加检验
                ;
            }

        }
        for(int i=0; i<infoList.size();i++){
            int row = table->rowCount();
            for(int j = 0; j < row; j++){
                QTableWidgetItem  *item = table->item( j,  1);          //通过行号和列号返回项
                QString src = item->data(Qt::UserRole).toString();
                if(src.isEmpty()){
                    QTableWidgetItem  *itemCheck = table->item( j,  0);
                    itemCheck->setCheckState(Qt::Checked);
                    item->setData(Qt::UserRole,QVariant(infoList[i].absoluteFilePath()));
                    QString text = infoList[i].fileName();
                    QString textBrowserString = "add file:" + infoList[i].absoluteFilePath();
                    if(index == 0){                 //hex2bin
                        this->ui->textBrowser->append(textBrowserString);
                    }else if(index == 1){           //binMerge
                        this->ui->textBrowser_2->append(textBrowserString);
                    }else if(index == 2){           //添加检验
                        ;
                    }
                    item->setText(text);
                    qDebug() << "add file:" << infoList[i].fileName() << "to table";
                    break;
                }
            }
        }


    }

}

/**
 * @brief MainWindow::on_actionDeleteAllRow_triggered   清空表格
 */
void MainWindow::on_actionDeleteAllRow_triggered()
{
    QTableWidget* table = getTableObjectByTabIndex();
    if(table == nullptr)return;
    int rowCount = table->rowCount();
    for(int i = 0;i < rowCount;i++){
        table->removeRow(0);
    }
}

/**
 * @brief MainWindow::on_tableWidget_BinMerge_cellDoubleClicked binMerge表格信号处理函数
 * @param row
 * @param column
 */
void MainWindow::on_tableWidget_BinMerge_cellDoubleClicked(int row, int column)
{
    QTableWidget* table = getTableObjectByTabIndex();
    if(table == nullptr)return;
    QTableWidgetItem  *item = table->item( row,  column);          //通过行号和列号返回项
    int cellType= item->type();
    if(cellType == MainWindow::cSelect){    //选择
        Qt::CheckState isCheck = item->checkState();
        if (isCheck != Qt::Checked)
            item->setCheckState(Qt::Checked);
        else
            item->setCheckState(Qt::Unchecked);
    }else if(cellType == MainWindow::cInput){//输入bin
        QStringList fileList = getFileListFromFolder();
        int size = fileList.size();
        int index = 0;
        for(;index < size;index++){
            if(index != 0){
                table->insertRow(row+index);
                tabBinMergeCreateItemARow (row+index,true);
                item = table->item( row+index,  1);
            }else{
                QTableWidgetItem  *itemCheck = table->item( row,  0);
                itemCheck->setCheckState(Qt::Checked);
            }
            item->setData(Qt::UserRole,QVariant(fileList[index]));
            QFileInfo  fileInfo(fileList[index]);
            QString text = fileInfo.fileName();
            QString textBrowserString = "add file:" + fileList[index];
            this->ui->textBrowser_2->append(textBrowserString);
            // this->ui->textBrowser->setPlainText(text);
            item->setText(text);
        }

    }else if(cellType == MainWindow::cOutput){      //打开bin
        QString dest = item->data(Qt::UserRole).toString();
        if(dest.isEmpty())return;
        QFileInfo  fileInfo(dest);
        QDir::setCurrent(fileInfo.absolutePath());  //设置应用程序当前目录
        QUrl url = QUrl(QDir::currentPath(), QUrl::TolerantMode);
        QDesktopServices::openUrl(url);
    }else{
        ;
    }
}

/**
 * @brief MainWindow::on_pushButton_BinMerge_clicked 开始合并文件
 */
void MainWindow::on_pushButton_BinMerge_clicked()
{
    on_actionTransitioningContent_triggered();
}

/**
 * @brief MainWindow::on_binMergeOpenFloder_PushButton_clicked 打开合并文件所在的文件夹
 */
void MainWindow::on_binMergeOpenFloder_PushButton_clicked()
{
    QString readS = binMergeOpenFloder_PushButton->property("binMergeFilePath").toString();
    if(readS.isEmpty())return;
    QFileInfo  fileInfo(readS);
    QUrl url = QUrl(fileInfo.absolutePath(), QUrl::TolerantMode);
    QDesktopServices::openUrl(url);
}

/**
 * @brief MainWindow::UI_Init   初始化UI
 */
void MainWindow::UI_Init()
{
    ui->tableWidget_Hex2Bin->setAlternatingRowColors(true);
    tabHex2BinInit();
    tabBinMergeInit();
    QHBoxLayout *layout=new QHBoxLayout;
    layout->setContentsMargins(0,0,0,0);
    layout->addWidget(ui->splitter);
    layout->addWidget(ui->groupBox);
    ui->tabHex2Bin->setLayout(layout);
    QHBoxLayout *layoutBinMerge=new QHBoxLayout;
    layoutBinMerge->setContentsMargins(0,0,0,0);
    layoutBinMerge->addWidget(ui->splitter_2);
    layoutBinMerge->addWidget(ui->groupBox_2);
    ui->tabMergeBin->setLayout(layoutBinMerge);
    binMergeOpenFloder_PushButton = new QPushButton(ui->groupBox_2);
    binMergeOpenFloder_PushButton->setObjectName("binMergeOpenFloder_PushButton");
    QIcon icon;
    icon.addFile(QString::fromUtf8(":/icon/image/OpenFolder.png"), QSize(), QIcon::Normal, QIcon::Off);
    binMergeOpenFloder_PushButton->setIcon(icon);
    binMergeOpenFloder_PushButton->setText(tr("打开文件夹"));
    binMergeOpenFloder_PushButton->setToolTip(tr("打开合并文件所在的文件夹"));
    binMergeOpenFloder_PushButton->setStatusTip(tr("打开合并文件所在的文件夹"));
    binMergeOpenFloder_PushButton->setEnabled(false);
    ui->gridLayout_2->addWidget(binMergeOpenFloder_PushButton, 1, 0, 1, 1);
    QMainWindow::setCentralWidget(this->ui->tabWidget);
}

/**
 * @brief MainWindow::connectSignalsSlots   槽与信号连接
 */
void MainWindow::connectSignalsSlots()
{
    connect(binMergeOpenFloder_PushButton, SIGNAL(clicked()), this, SLOT(on_binMergeOpenFloder_PushButton_clicked()));
}




